home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTPREV.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  103 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btprev.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btprev - previous btree key
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btprev(btp)
  20.      btree_t *btp;
  21.  
  22. DESCRIPTION
  23.      The btprev function retreats the cursor of btree btp to the
  24.      previous key.  If cursor is currently null, it will be moved to
  25.      the last key.  If the cursor is currently on the last key, it
  26.      will move to null.  If the tree is empty, the cursor will remain
  27.      on null.
  28.  
  29.      btprev will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       btp is not a valid btree pointer.
  32.      [BTELOCK]      btp is not locked.
  33.      [BTENOPEN]     btp is not open.
  34.  
  35. SEE ALSO
  36.      btcursor, btfirst, btlast, btnext.
  37.  
  38. DIAGNOSTICS
  39.      Upon successful completion, a value of 0 is returned.  Otherwise,
  40.      a value of -1 is returned, and errno set to indicate the error.
  41.  
  42. ------------------------------------------------------------------------------*/
  43. int btprev(btp)
  44. btree_t *btp;
  45. {
  46.     int terrno = 0;        /* tmp errno */
  47.  
  48.     /* validate arguments */
  49.     if (!bt_valid(btp)) {
  50.         errno = EINVAL;
  51.         return -1;
  52.     }
  53.  
  54.     /* check if not open */
  55.     if (!(btp->flags & BTOPEN)) {
  56.         errno = BTENOPEN;
  57.         return -1;
  58.     }
  59.  
  60.     /* check locks */
  61.     if (!(btp->flags & BTLOCKS)) {
  62.         errno = BTELOCK;
  63.         return -1;
  64.     }
  65.  
  66.     /* move cursor to previous key in current node */
  67.     if (btp->cbtpos.node != NIL) {
  68.         if (--btp->cbtpos.key > 0) {
  69.             errno = 0;
  70.             return 0;
  71.         }
  72.     }
  73.  
  74.     /* move cursor to null */
  75.     if (btp->cbtpos.node == btp->bthdr.first) {
  76.         btp->cbtpos.node = NIL;
  77.         btp->cbtpos.key = 0;
  78.         bt_ndinit(btp, btp->cbtnp);
  79.         errno = 0;
  80.         return 0;
  81.     }
  82.  
  83.     /* move cursor to last key in prev node */
  84.     if (btp->cbtpos.node == NIL) {
  85.         btp->cbtpos.node = btp->bthdr.last;
  86.     } else {
  87.         btp->cbtpos.node = btp->cbtnp->lsib;
  88.     }
  89.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  90.         BTEPRINT;
  91.         terrno = errno;
  92.         btp->cbtpos.node = NIL;
  93.         btp->cbtpos.key = 0;
  94.         bt_ndinit(btp, btp->cbtnp);
  95.         errno = terrno;
  96.         return -1;
  97.     }
  98.     btp->cbtpos.key = btp->cbtnp->n;
  99.  
  100.     errno = 0;
  101.     return 0;
  102. }
  103.